package com.zh.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zh.common.vo.PageDTO;
import com.zh.common.vo.PageQuery;
import com.zh.common.vo.Result;
import com.zh.modules.app.entity.OrdersDO;
import com.zh.modules.app.entity.vo.OrdersVO;
import com.zh.modules.app.param.OrderQueryParam;
import com.zh.modules.app.repository.OrdersRepository;
import com.zh.modules.app.service.OrderService;
import com.zh.modules.app.utils.Condition;
import com.zh.modules.app.utils.JwtUtils;
import com.zh.modules.app.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/app/order")
@Api(tags = "订单")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrdersRepository repository;

    @PostMapping("/queryUnitPrice")
    @ApiOperation("查询单价")
    public Result<Integer> queryUnitPrice(@RequestBody OrdersVO ordersVO) {
        Integer unitPrice = orderService.queryUnitPrice(ordersVO);
        return Result.succeed(unitPrice);
    }


    @PostMapping("/createOrder")
    @ApiOperation("提交订单")
    public Result<Boolean> createOrder(@RequestBody OrdersVO ordersVO) {
        orderService.createOrder(ordersVO);
        return Result.succeed(true);
    }


    @PostMapping("/query")
    @ApiOperation("列表查询")
    public Result<PageDTO<OrdersDO>> query(@RequestBody OrderQueryParam param) {
        Integer userId = JwtUtils.getUser().getId();
        // 创建分页对象
        PageQuery query = new PageQuery();
        query.setPageSize(param.getPageSize());
        query.setPageNo(param.getPageNo());
        LambdaQueryWrapper<OrdersDO> queryWrapper = new LambdaQueryWrapper<OrdersDO>().eq(OrdersDO::getUserId, userId);
        IPage<OrdersDO> ordersDOIPage = repository.page(Condition.getPage(query), queryWrapper);
        return Result.succeed(PageUtils.convert2PageDTO(ordersDOIPage, null));
    }

}
